package com.redis;

import com.alibaba.fastjson.TypeReference;
import com.redis.config.RedisConfig;
import com.redis.exception.AppException;
import com.redis.utils.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.Date;

public class RedisClient implements Redis {
    protected JedisPool pool;
    protected static final int DEFAULT_MAX_TOTAL = 500;
    protected static final int DEFAULT_MAX_IDLE = 20;
    protected static final int DEFAULT_MAX_WAIT = 3500;
    protected static final int DEFAULT_TIME_OUT = 3000;
    protected static final int DEFAULT_DATABASE = 0;
    protected RedisConfig config;

    public RedisClient() {
    }

    public RedisClient(RedisConfig config) {
        this.config = config;
    }

    public Jedis getClient() {
        if (this.pool == null) {
            synchronized(this) {
                if (this.pool == null) {
                    try {
                        RedisConfig cacheConfig = this.config;
                        if (this.config == null) {
                            throw new Exception("请先初始化redis服务器配置");
                        }

                        String host = cacheConfig.getHost();
                        int port = cacheConfig.getPort();
                        String password = cacheConfig.getPassword();
                        int timeOut = cacheConfig.getTimeOut() > 0 ? cacheConfig.getTimeOut() : 3000;
                        JedisPoolConfig config = new JedisPoolConfig();
                        config.setMaxTotal(cacheConfig.getMaxTotal() > 0 ? cacheConfig.getMaxTotal() : 500);
                        config.setMaxIdle(cacheConfig.getMaxIdle() > 0 ? cacheConfig.getMaxIdle() : 20);
                        config.setMaxWaitMillis(cacheConfig.getMaxWait() > 0 ? (long)cacheConfig.getMaxWait() : 3500L);
                        config.setTestOnBorrow(true);
                        this.pool = new JedisPool(config, host, port, timeOut, password, cacheConfig.getDatabase());
                    } catch (Exception var9) {
                        throw new AppException(var9.getMessage(), var9);
                    }
                }
            }
        }

        return this.pool.getResource();
    }

    private void closeClient(Jedis client) {
        if (client != null) {
            client.close();
        }

    }

    public <T> void set(String key, T object) {
        this.set(key, object, 0);
    }

    public <T> void set(String key, T object, int expireSecond) {
        Jedis client = this.getClient();
        Throwable var5 = null;

        try {
            String value = object instanceof String ? (String)object : StringUtils.objectToJson(object);
            if (expireSecond <= 0) {
                client.set(key, value);
            } else {
                client.setex(key, expireSecond, value);
            }
        } catch (Throwable var14) {
            var5 = var14;
            throw var14;
        } finally {
            if (client != null) {
                if (var5 != null) {
                    try {
                        client.close();
                    } catch (Throwable var13) {
                        var5.addSuppressed(var13);
                    }
                } else {
                    client.close();
                }
            }

        }

    }

    public <T> void set(String key, T object, Date expireDate) {
        Jedis client = this.getClient();
        Throwable var5 = null;

        try {
            Date nowDate = new Date();
            long expireSecond = (expireDate.getTime() - nowDate.getTime()) / 1000L;
            String value = object instanceof String ? (String)object : StringUtils.objectToJson(object);
            if (expireSecond <= 0L) {
                client.set(key, value);
            } else {
                client.setex(key, (int)expireSecond, value);
            }
        } catch (Throwable var17) {
            var5 = var17;
            throw var17;
        } finally {
            if (client != null) {
                if (var5 != null) {
                    try {
                        client.close();
                    } catch (Throwable var16) {
                        var5.addSuppressed(var16);
                    }
                } else {
                    client.close();
                }
            }

        }

    }

    public String getString(String key) {
        Jedis client = this.getClient();
        Throwable var3 = null;

        String var5;
        try {
            String value = client.get(key);
            var5 = value;
        } catch (Throwable var14) {
            var3 = var14;
            throw var14;
        } finally {
            if (client != null) {
                if (var3 != null) {
                    try {
                        client.close();
                    } catch (Throwable var13) {
                        var3.addSuppressed(var13);
                    }
                } else {
                    client.close();
                }
            }

        }

        return var5;
    }

    public <T> T get(String key, Class<T> t) {
        String value = this.getString(key);
        T object = "String".equals(t.getSimpleName()) ? (T) value : StringUtils.jsonToObjcet(value, t);
        return object;
    }

    public <T> T get(String key, TypeReference<T> typeReference) {
        String value = this.getString(key);
        T object = StringUtils.jsonToObjcet(value, typeReference);
        return object;
    }

    public boolean exists(String key) {
        Jedis client = this.getClient();
        Throwable var3 = null;

        boolean var4;
        try {
            var4 = client.exists(key);
        } catch (Throwable var13) {
            var3 = var13;
            throw var13;
        } finally {
            if (client != null) {
                if (var3 != null) {
                    try {
                        client.close();
                    } catch (Throwable var12) {
                        var3.addSuppressed(var12);
                    }
                } else {
                    client.close();
                }
            }

        }

        return var4;
    }

    public long exists(String... keys) {
        Jedis client = this.getClient();
        Throwable var3 = null;

        long var4;
        try {
            var4 = client.exists(keys);
        } catch (Throwable var14) {
            var3 = var14;
            throw var14;
        } finally {
            if (client != null) {
                if (var3 != null) {
                    try {
                        client.close();
                    } catch (Throwable var13) {
                        var3.addSuppressed(var13);
                    }
                } else {
                    client.close();
                }
            }

        }

        return var4;
    }

    public long delete(String key) {
        Jedis client = this.getClient();
        Throwable var3 = null;

        long var6;
        try {
            long result = client.del(key);
            var6 = result;
        } catch (Throwable var16) {
            var3 = var16;
            throw var16;
        } finally {
            if (client != null) {
                if (var3 != null) {
                    try {
                        client.close();
                    } catch (Throwable var15) {
                        var3.addSuppressed(var15);
                    }
                } else {
                    client.close();
                }
            }

        }

        return var6;
    }

    public long delete(String... keys) {
        Jedis client = this.getClient();
        Throwable var3 = null;

        long var6;
        try {
            long result = client.del(keys);
            var6 = result;
        } catch (Throwable var16) {
            var3 = var16;
            throw var16;
        } finally {
            if (client != null) {
                if (var3 != null) {
                    try {
                        client.close();
                    } catch (Throwable var15) {
                        var3.addSuppressed(var15);
                    }
                } else {
                    client.close();
                }
            }

        }

        return var6;
    }

    public long expire(String key, int seconds) {
        Jedis client = this.getClient();
        Throwable var4 = null;

        long var5;
        try {
            var5 = client.expire(key, seconds);
        } catch (Throwable var15) {
            var4 = var15;
            throw var15;
        } finally {
            if (client != null) {
                if (var4 != null) {
                    try {
                        client.close();
                    } catch (Throwable var14) {
                        var4.addSuppressed(var14);
                    }
                } else {
                    client.close();
                }
            }

        }

        return var5;
    }

    public long setnx(String key, String value) {
        Jedis client = this.getClient();
        Throwable var4 = null;

        long var5;
        try {
            var5 = client.setnx(key, value);
        } catch (Throwable var15) {
            var4 = var15;
            throw var15;
        } finally {
            if (client != null) {
                if (var4 != null) {
                    try {
                        client.close();
                    } catch (Throwable var14) {
                        var4.addSuppressed(var14);
                    }
                } else {
                    client.close();
                }
            }

        }

        return var5;
    }

    public String getSet(String key, String value) {
        Jedis client = this.getClient();
        Throwable var4 = null;

        String var5;
        try {
            var5 = client.getSet(key, value);
        } catch (Throwable var14) {
            var4 = var14;
            throw var14;
        } finally {
            if (client != null) {
                if (var4 != null) {
                    try {
                        client.close();
                    } catch (Throwable var13) {
                        var4.addSuppressed(var13);
                    }
                } else {
                    client.close();
                }
            }

        }

        return var5;
    }

    public String set(String key, String value, String nxxx, String expx, long time) {
        Jedis client = this.getClient();
        Throwable var8 = null;

        String var9;
        try {
            var9 = client.set(key, value, nxxx, expx, time);
        } catch (Throwable var18) {
            var8 = var18;
            throw var18;
        } finally {
            if (client != null) {
                if (var8 != null) {
                    try {
                        client.close();
                    } catch (Throwable var17) {
                        var8.addSuppressed(var17);
                    }
                } else {
                    client.close();
                }
            }

        }

        return var9;
    }
}
